<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SessionsController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest',[
           'only' => ['create']
        ]);
    }
    public function create()
    {
        return view('sessions.create');
    }

    public function store(Request $request)
    {
        $cc=$this->validate($request,[
           'email' => 'required|email',
           'password' => 'required'
        ]);
        if(Auth::attempt($cc,$request->has('remember'))){
            if(Auth::user()->status) {
                session()->flash('success', '登录成功！');
                return redirect()->intended(route('users.show', Auth::user()));
            }else{
                Auth::logout();
                session()->flash('warning','请先激活用户!');
                return back();
            }
        }else{
            session()->flash('danger','不匹配！');
            return back();
        }
    }

    public function destroy()
    {
        Auth::logout();
        session()->flash('success','登出成功！');
        return redirect()->route('login');
    }
}
